import 'package:flutter/material.dart';
import 'package:x3_base_core/base_theme/base_body_theme.dart';
import 'package:x3_component_core/theme/base_button_theme.dart';

class BaseButton extends StatelessWidget {
  /// 普通按钮 宽度为 100% 高度为 40.0
  const BaseButton({Key key, @required this.text, this.disable = false, this.onPress, this.circular = 3.0, this.color, this.fill = true, this.width = double.infinity, this.height = 40.0, this.isAdaptive = false, this.isBottom = false, this.borderColor}) : super(key: key);

  /// 改按钮如果是页面底部/页脚位置的按钮 则调用该构造器
  const BaseButton.bottom({Key key, @required this.text, this.disable = false, this.onPress, this.circular = 4.0, this.color, this.fill = true, this.width = double.infinity, this.height = 40.0, this.isAdaptive = false, this.isBottom = true, this.borderColor}) : super(key: key);

  /// 2个字的按钮 宽度为 64.0 高度 32
  const BaseButton.two({Key key, @required this.text, this.disable = false, this.onPress, this.circular = 3.0, this.color, this.fill = false, this.width = 64.0, this.height = 32.0, this.isAdaptive = false, this.isBottom = false, this.borderColor}) : super(key: key);

  /// 3个字的按钮 宽度为 80.0 高度 32
  const BaseButton.three({Key key, @required this.text, this.disable = false, this.onPress, this.circular = 3.0, this.color, this.fill = false, this.width = 80.0, this.height = 32.0, this.isAdaptive = false, this.isBottom = false, this.borderColor}) : super(key: key);

  /// 4个字的按钮 宽度为 88.0 高度 32
  const BaseButton.four({Key key, @required this.text, this.disable = false, this.onPress, this.circular = 3.0, this.color, this.fill = false, this.width = 88.0, this.height = 32.0, this.isAdaptive = false, this.isBottom = false, this.borderColor}) : super(key: key);

  /// 5个字的按钮 宽度为 100.0 高度 32
  const BaseButton.five({Key key, @required this.text, this.disable = false, this.onPress, this.circular = 3.0, this.color, this.fill = false, this.width = 102.0, this.height = 32.0, this.isAdaptive = false, this.isBottom = false, this.borderColor}) : super(key: key);

  /// 6个字的按钮 宽度为 108.0 高度 32 这个不是在规范里面的
  const BaseButton.six({Key key, @required this.text, this.disable = false, this.onPress, this.circular = 3.0, this.color, this.fill = false, this.width = 108.0, this.height = 32.0, this.isAdaptive = false, this.isBottom = false, this.borderColor}) : super(key: key);

  final String text;

  final bool disable;

  final VoidCallback onPress;

  final double circular;

  final Color color;

  final bool fill;

  final double height;

  final double width;

  /// 是底部按钮，由于底部按钮比较高，而且
  final bool isBottom;

  /// 自适应宽度 当isAdaptive为true时候，不再按照规范来设置宽度
  final bool isAdaptive;

  /// 边框颜色
  final Color borderColor;

  @override
  Widget build(BuildContext context) {
    Color primaryColor = Theme.of(context).primaryColor == Colors.white ? BaseBodyTheme.textColor : Theme.of(context).primaryColor;
    Color initBackGroundColor = this.color == null ? primaryColor : this.color;
    Color backGroundColor = this.fill == true ? initBackGroundColor : Colors.white;
    Color textColor = this.fill == true ? Colors.white : initBackGroundColor;
    Color borderColor = this.borderColor != null
        ? this.borderColor
        : Theme.of(context).primaryColor == Colors.white
            ? Color(0xFFDFDFDF)
            : Theme.of(context).primaryColor;
    if (this.disable) {
      textColor = textColor.withAlpha(80);
      borderColor = borderColor.withAlpha(80);
      backGroundColor = backGroundColor.withAlpha(80);
    }
    if (isAdaptive)
      return InkWell(
        onTap: disable == false ? onPress : null,
        child: Container(
          decoration: BoxDecoration(border: Border.all(width: 0.8, color: borderColor), borderRadius: BorderRadius.circular(this.circular), color: backGroundColor),
          constraints: BoxConstraints(minWidth: 56, maxHeight: 32.0, minHeight: 32.0),
          padding: EdgeInsets.symmetric(horizontal: 8),
          alignment: Alignment.center,
          child: Text(text, style: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: BaseButtonTheme.buttonTextSize, color: textColor)),
        ),
      );
    return Container(
      width: this.width,
      height: this.height,
      child: RaisedButton(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(this.circular), side: BorderSide(color: borderColor)),
        color: backGroundColor,
        disabledColor: backGroundColor,
        textColor: textColor,
        disabledTextColor: textColor.withAlpha(80),
        elevation: 0,
        disabledElevation: 0,
        highlightElevation: 0,
        onPressed: disable == false ? onPress : null,
        child: Text(text, style: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: this.isBottom ? BaseButtonTheme.bottomButtonTextSize : BaseButtonTheme.buttonTextSize, color: textColor)),
      ),
    );
  }
}
